package com.juku2024.juku

import android.os.Bundle
import android.view.View
import com.rabbit.videoplayer.tools.VideoPlayerSPUtil
import com.juku2024.juku.base.BaseActivity
import com.juku2024.juku.callback.OnNoDoubleClickListener
import com.juku2024.juku.model.PlayerDecodeEvent
import org.greenrobot.eventbus.EventBus

class PlayerDecodeActivity: BaseActivity(), OnNoDoubleClickListener {

    //是否准备选择内部存储
    private var isEnableHardDecoder: Boolean = true

    private lateinit var layoutPlayerDecodeHard: View
    private lateinit var layoutPlayerDecodeSoft: View
    private lateinit var ivPlayerDecodeHard: View
    private lateinit var ivPlayerDecodeSoft: View

    override fun getLayoutResId(): Int {
        return R.layout.fragment_player_setting
    }

    override fun initView() {
        super.initView()
        layoutPlayerDecodeHard = findViewById(R.id.layout_player_decode_hard)
        layoutPlayerDecodeSoft = findViewById(R.id.layout_player_decode_soft)
        ivPlayerDecodeHard = findViewById(R.id.iv_player_decode_hard)
        ivPlayerDecodeSoft = findViewById(R.id.iv_player_decode_soft)
    }

    override fun bindEvent() {
        super.bindEvent()
        layoutPlayerDecodeHard.setOnClickListener(this)
        layoutPlayerDecodeSoft.setOnClickListener(this)
    }

    override fun init(savedInstanceState: Bundle?) {
        // 初始化内部存储的空间
        isEnableHardDecoder = VideoPlayerSPUtil.instance.isEnableHardDecoder
        setEnableHardDecoder()
    }

    override fun onNoDoubleClick(v: View?) {
        super.onNoDoubleClick(v)
        when (v) {
            layoutPlayerDecodeHard -> {
                if (isEnableHardDecoder) return
                // 选中内部存储，不实用sdcard
                isEnableHardDecoder = true
                setEnableHardDecoder()
            }
            layoutPlayerDecodeSoft -> {
                if (!isEnableHardDecoder) return
                // 选中sdcard存储，不实用inner
                isEnableHardDecoder = false
                setEnableHardDecoder()
            }
        }
    }

    // 设置存储路径
    private fun setEnableHardDecoder() {
        if (isEnableHardDecoder) {
            ivPlayerDecodeHard.setBackgroundResource(R.drawable.ic_checked)
            ivPlayerDecodeSoft.setBackgroundResource(R.drawable.ic_check_normal)
        } else {
            ivPlayerDecodeHard.setBackgroundResource(R.drawable.ic_check_normal)
            ivPlayerDecodeSoft.setBackgroundResource(R.drawable.ic_checked)
        }
        VideoPlayerSPUtil.instance.isEnableHardDecoder = isEnableHardDecoder

        EventBus.getDefault().post(PlayerDecodeEvent(isEnableHardDecoder))
    }

}